package com.example.demo.model


import xyz.erupt.annotation.*
import xyz.erupt.annotation.`fun`.*
import xyz.erupt.annotation.sub_field.*
import xyz.erupt.annotation.sub_field.sub_edit.*
import xyz.erupt.core.exception.*
import xyz.erupt.jpa.model.*
import javax.persistence.*


/**
 * see [link](https://www.yuque.com/erupts/erupt/nicqg3)
 */
@Erupt(
    name = "服务层逻辑扩展 @DataProxy （扩展CURD）",
    desc = """提供增、删、改、查、导入、导出、数据初始化等事件触发逻辑接口，相当于传统开发中的 service 层
              可以实现如：缓存写入，数据校验，RPC调用，动态赋值等功能 ！）""",
    dataProxy = [CURDExtension::class]
)
@Entity
@Table(name = "demo_curd_extension")
@Suppress("unused")
class CURDExtension : BaseModel(), DataProxy<CURDExtension?> {

    @EruptField(views = [View(title = "名称")], edit = Edit(title = "名称", notNull = true, search = Search(vague = true)))
    var name: String = ""

    override fun beforeAdd(curdExtension: CURDExtension?) {
        checkNotNull(curdExtension) { return }
        //字段校验
        if ("张三" == curdExtension.name) {
            throw EruptApiErrorTip("名称禁止为张三！")
        }
    }

    override fun afterAdd(curdExtension: CURDExtension?) {
        //TODO 添加完成后处理逻辑
    }

    override fun beforeUpdate(curdExtension: CURDExtension?) {
        checkNotNull(curdExtension) { return }
        //动态写数据
        curdExtension.name = "${curdExtension.name}xxx"
    }

    override fun afterUpdate(curdExtension: CURDExtension?) {
        //TODO 修改完成后处理逻辑
    }

    override fun beforeDelete(curdExtension: CURDExtension?) {
        //TODO 删除前事件处理逻辑
    }

    override fun afterDelete(curdExtension: CURDExtension?) {
        //TODO 删除后事件处理逻辑
    }

    override fun afterFetch(list: Collection<Map<String, Any>>) {
        //TODO 查询结果处理
    }

    override fun addBehavior(curdExtension: CURDExtension?) {
        //TODO 数据初始化逻辑
    }

    override fun editBehavior(curdExtension: CURDExtension?) {
        //TODO 编辑时数据处理逻辑
    }
}